#!/usr/bin/env Rscript
##################################
############ Preamble ############
##################################
# set language to English
Sys.setenv(LANG = "en")

# clean up
rm(list = ls())

# Set working directory: Please set your own
if (Sys.getenv("RSTUDIO") == "1") setwd("~/Dropbox/cues_bjps_replication")

library(Rmisc)
library(tidyverse)
library(readxl)
library(stargazer)
library(kableExtra)
library(sandwich)
library(broom)
library(dotwhisker)
library(jtools)
library(scales)



# read data
raw_data <- read_xlsx("data/final_survey_data.xlsx")

# rename variables
raw_data <- raw_data %>%
  dplyr::rename(
    number = lfdn,
    completion_time = duration,
    disposition_code = dispcode,
    treatment = c_0031,
    cover_page = v_27,
    age = v_28,
    age_range = v_29,
    gender = v_30,
    state = v_31,
    born_in_germany = v_39,
    sign_petition_self = v_47,
    sensitive_item_agree = dupl1_v_44,
    left_right = dupl1_v_41,
    petition_appropriate_self = v_48,
    sign_petition_others = v_49,
    petition_appropriate_others = v_50,
    delete_tweet = v_51,
    voted_2021 = v_53,
    party_voted_2021 = v_54,
    party_voted_2021_other_which = v_55,
    voted_2017 = v_56,
    party_voted_2017 = v_57,
    party_voted_2017_other_which = v_58,
    household_income = v_59,
    education = v_60,
    mother_born_where = v_61,
    mother_born_other_where = v_62,
    father_born_where = v_63,
    father_born_other_where = v_64
  )

raw_data <- raw_data %>%
  mutate(treatment = replace(treatment, which(treatment == 1), "Mainstream Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 2), "Mainstream Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 3), "RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 4), "RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 5), "Mainstream and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 6), "Mainstream and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 7), "Mainstream Disapprove and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 8), "Mainstream Disapprove and RRP Approve")) %>%
  mutate(treatment = replace(treatment, which(treatment == 9), "Control")) %>%
  mutate(treatment = replace(treatment, which(treatment == 10), "Control"))

# recode state names
raw_data <- raw_data %>%
  mutate(state = replace(state, which(state == 1), "Baden-Wuerttemberg")) %>%
  mutate(state = replace(state, which(state == 2), "Bayern")) %>%
  mutate(state = replace(state, which(state == 3), "Berlin")) %>%
  mutate(state = replace(state, which(state == 4), "Brandenburg")) %>%
  mutate(state = replace(state, which(state == 5), "Bremen")) %>%
  mutate(state = replace(state, which(state == 6), "Hamburg")) %>%
  mutate(state = replace(state, which(state == 7), "Hessen")) %>%
  mutate(state = replace(state, which(state == 8), "Mecklenburg-Vorpommern")) %>%
  mutate(state = replace(state, which(state == 9), "Niedersachsen")) %>%
  mutate(state = replace(state, which(state == 10), "Nordrhein-Westfalen")) %>%
  mutate(state = replace(state, which(state == 11), "Rheinland-Pfalz")) %>%
  mutate(state = replace(state, which(state == 14), "Sachsen-Anhalt")) %>%
  mutate(state = replace(state, which(state == 12), "Saarland")) %>%
  mutate(state = replace(state, which(state == 13), "Sachsen")) %>%
  mutate(state = replace(state, which(state == 15), "Schleswig-Holstein")) %>%
  mutate(state = replace(state, which(state == 16), "Thueringen"))

# recode age_ranges
raw_data <- raw_data %>%
  mutate(age_range = replace(age_range, which(age_range == 1), "18 - 29 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 2), "30 - 39 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 3), "40 - 49 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 4), "50 - 59 years")) %>%
  mutate(age_range = replace(age_range, which(age_range == 5), "60 - 90 years"))

# recode gender
raw_data <- raw_data %>%
  mutate(gender = replace(gender, which(gender == 1), "Male")) %>%
  mutate(gender = replace(gender, which(gender == 2), "Female"))

# recode sensitive item - change sensitive item response "disagree" to 0
raw_data <- raw_data %>%
  mutate(sensitive_item_agree = replace(sensitive_item_agree, which(sensitive_item_agree == 2), 0))

# recode coordination game - change "not willing to sign" from 2 to 0
raw_data <- raw_data %>%
  mutate(sign_petition_self = replace(sign_petition_self, which(sign_petition_self == 2), 0))

# recode punishment exp - change "do not delete tweet" from 2 to 0
raw_data <- raw_data %>%
  mutate(delete_tweet = replace(delete_tweet, which(delete_tweet == 2), 0))

# recode left_right 100 is 0, 99 is NA
raw_data <- raw_data %>%
  mutate(left_right = replace(left_right, which(left_right == 100), 0)) %>%
  mutate(left_right = replace(left_right, which(left_right == 99), NA))

raw_data <- raw_data %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 1), "CDU/CSU")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 2), "SPD")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 3), "AfD")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 4), "Green")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 5), "FDP")) %>%
  mutate(party_voted_2021 = replace(party_voted_2021, which(party_voted_2021 == 6), "Die Linke"))

# remove incompletes or inattentives
analysis_data <- raw_data %>%
  filter(disposition_code == "31" | disposition_code == "32")

# clean up sensitive item - drop -77s, merge both columns
analysis_data <- analysis_data %>%
  mutate(sensitive_item_agree = replace(sensitive_item_agree, which(sensitive_item_agree < 0), NA))

# Create dummy variable
analysis_data$MRPapprovedummy <- ifelse(analysis_data$treatment == "Mainstream Approve", 1, 0)
analysis_data$RRPapprovedummy <- ifelse(analysis_data$treatment == "RRP Approve", 1, 0)
analysis_data$MRPapproveRRPapprovedummy <- ifelse(analysis_data$treatment == "Mainstream and RRP Approve", 1, 0)
analysis_data$MRPdisapproveRRPapprovedummy <- ifelse(analysis_data$treatment == "Mainstream Disapprove and RRP Approve", 1, 0)




# cleaning

state_names <- c(
  "Baden-Wuerttemberg",
  "Bayern",
  "Berlin",
  "Brandenburg",
  "Bremen",
  "Hamburg",
  "Hessen",
  "Mecklenburg-Vorpommern",
  "Niedersachsen",
  "Nordrhein-Westfalen",
  "Rheinland-Pfalz",
  "Sachsen-Anhalt",
  "Saarland",
  "Sachsen",
  "Schleswig-Holstein",
  "Thueringen"
)

for (i in 1:16) {
  # subset analysis data
  analysis_data_remove_baden <- analysis_data %>%
    filter(state != state_names[i])

  # mainstream only
  mainstream_only_remove_baden <- analysis_data_remove_baden %>%
    filter(party_voted_2021 == "CDU/CSU" | party_voted_2021 == "SPD" | party_voted_2021 == "Green" | party_voted_2021 == "FDP")

  # RRP only
  # rrp_only_remove_baden <- analysis_data_remove_baden %>%
  #   filter(party_voted_2021 == "AfD")

  # right-wing only
  right_only_remove_baden <- analysis_data_remove_baden %>%
    filter(party_voted_2021 == "CDU/CSU" | party_voted_2021 == "FDP" | party_voted_2021 == "AfD")

  # left-wing only
  left_only_remove_baden <- analysis_data_remove_baden %>%
    filter(party_voted_2021 == "SPD" | party_voted_2021 == "Green" | party_voted_2021 == "Die Linke")


  # standardize by subtracting mean and dividing by SD
  analysis_data_remove_baden[, c(18, 19, 20, 21, 22, 23)] <- scale(analysis_data_remove_baden[, c(18, 19, 20, 21, 22, 23)])
  left_only_remove_baden[, c(18, 19, 20, 21, 22, 23)] <- scale(left_only_remove_baden[, c(18, 19, 20, 21, 22, 23)])
  right_only_remove_baden[, c(18, 19, 20, 21, 22, 23)] <- scale(right_only_remove_baden[, c(18, 19, 20, 21, 22, 23)])
  # rrp_only_remove_baden[,c(18,19,20,21,22,23)]<-scale(rrp_only_remove_baden[,c(18,19,20,21,22,23)])


  # Sensitive Item - Male Only

  # regression for Agreement with Sensitive Item across different samples
  sensitive_item_controls_fullsample_remove_baden <- lm(sensitive_item_agree ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_remove_baden)
  sensitive_item_controls_leftonly_remove_baden <- lm(sensitive_item_agree ~ MRPapprovedummy * RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_remove_baden)
  sensitive_item_controls_rightonly_remove_baden <- lm(sensitive_item_agree ~ MRPapprovedummy * RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_remove_baden)
  # sensitive_item_controls_rrponly_remove_baden <- lm(sensitive_item_agree ~ MRPapprovedummy *RRPapprovedummy+ MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_remove_baden)

  # robust SEs - "HC1" for STATA
  robust_sensitive_item_controls_fullsample_remove_baden <- summ(sensitive_item_controls_fullsample_remove_baden, robust = "HC1")
  robust_sensitive_item_controls_leftonly_remove_baden <- summ(sensitive_item_controls_leftonly_remove_baden, robust = "HC1")
  robust_sensitive_item_controls_rightonly_remove_baden <- summ(sensitive_item_controls_rightonly_remove_baden, robust = "HC1")
  # robust_sensitive_item_controls_rrponly_remove_baden <- summ(sensitive_item_controls_rrponly_remove_baden, robust = "HC1")

  # convert regression outcomes to df for coefficient plot
  robust_sensitive_item_controls_fullsample_remove_baden_df <- broom::tidy(robust_sensitive_item_controls_fullsample_remove_baden) %>%
    mutate(sample = "Full Sample") %>%
    mutate(measure = "Agreement with Sensitive Item") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sensitive_item_controls_fullsample_remove_baden_df)[1] <- "model"
  names(robust_sensitive_item_controls_fullsample_remove_baden_df)[7] <- "term"

  robust_sensitive_item_controls_leftonly_remove_baden_df <- broom::tidy(robust_sensitive_item_controls_leftonly_remove_baden) %>%
    mutate(sample = "Left Wing Only") %>%
    mutate(measure = "Agreement with Sensitive Item") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sensitive_item_controls_leftonly_remove_baden_df)[1] <- "model"
  names(robust_sensitive_item_controls_leftonly_remove_baden_df)[7] <- "term"


  robust_sensitive_item_controls_rightonly_remove_baden_df <- broom::tidy(robust_sensitive_item_controls_rightonly_remove_baden) %>%
    mutate(sample = "Right Wing Only") %>%
    mutate(measure = "Agreement with Sensitive Item") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sensitive_item_controls_rightonly_remove_baden_df)[1] <- "model"
  names(robust_sensitive_item_controls_rightonly_remove_baden_df)[7] <- "term"

  # robust_sensitive_item_controls_rrponly_remove_baden_df <- broom::tidy(robust_sensitive_item_controls_rrponly_remove_baden) %>%
  #   mutate(sample = "Radical Right Only") %>%
  #   mutate(measure = "Agreement with Sensitive Item") %>%
  #   filter(term != "age") %>%
  #   filter(term != "household_income")  %>%
  #   filter(term != "education")  %>%
  #   filter(term != "left_right")  %>%
  #   filter(term != "(Intercept)")
  #
  # #rename for the dwplot
  # names(robust_sensitive_item_controls_rrponly_remove_baden_df)[1] <- "model"
  # names(robust_sensitive_item_controls_rrponly_remove_baden_df)[7] <- "term"


  # Willingness to Sign Petition - remove_baden only


  # regression for Willingness to Sign Petition across different samples
  sign_petition_self_controls_fullsample_remove_baden <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_remove_baden)
  sign_petition_self_controls_leftonly_remove_baden <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_remove_baden)
  sign_petition_self_controls_rightonly_remove_baden <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_remove_baden)
  # sign_petition_self_controls_rrponly_remove_baden <- lm(sign_petition_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_remove_baden)

  # robust SEs
  robust_sign_petition_self_controls_fullsample_remove_baden <- summ(sign_petition_self_controls_fullsample_remove_baden, robust = "HC1")
  robust_sign_petition_self_controls_leftonly_remove_baden <- summ(sign_petition_self_controls_leftonly_remove_baden, robust = "HC1")
  robust_sign_petition_self_controls_rightonly_remove_baden <- summ(sign_petition_self_controls_rightonly_remove_baden, robust = "HC1")
  # robust_sign_petition_self_controls_rrponly_remove_baden <- summ(sign_petition_self_controls_rrponly_remove_baden, robust = "HC1")

  # convert regression outcomes to df for coefficient plot
  robust_sign_petition_self_controls_fullsample_remove_baden_df <- broom::tidy(robust_sign_petition_self_controls_fullsample_remove_baden) %>%
    mutate(sample = "Full Sample") %>%
    mutate(measure = "Personal Willingness to Sign Petition") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sign_petition_self_controls_fullsample_remove_baden_df)[1] <- "model"
  names(robust_sign_petition_self_controls_fullsample_remove_baden_df)[7] <- "term"

  robust_sign_petition_self_controls_leftonly_remove_baden_df <- broom::tidy(robust_sign_petition_self_controls_leftonly_remove_baden) %>%
    mutate(sample = "Left Wing Only") %>%
    mutate(measure = "Personal Willingness to Sign Petition") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sign_petition_self_controls_leftonly_remove_baden_df)[1] <- "model"
  names(robust_sign_petition_self_controls_leftonly_remove_baden_df)[7] <- "term"


  robust_sign_petition_self_controls_rightonly_remove_baden_df <- broom::tidy(robust_sign_petition_self_controls_rightonly_remove_baden) %>%
    mutate(sample = "Right Wing Only") %>%
    mutate(measure = "Personal Willingness to Sign Petition") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sign_petition_self_controls_rightonly_remove_baden_df)[1] <- "model"
  names(robust_sign_petition_self_controls_rightonly_remove_baden_df)[7] <- "term"

  # robust_sign_petition_self_controls_rrponly_remove_baden_df <- broom::tidy(robust_sign_petition_self_controls_rrponly_remove_baden) %>%
  #   mutate(sample = "Radical Right Only") %>%
  #   mutate(measure = "Personal Willingness to Sign Petition") %>%
  #   filter(term != "age") %>%
  #   filter(term != "household_income")  %>%
  #   filter(term != "education")  %>%
  #   filter(term != "left_right")%>%
  #   filter(term != "(Intercept)")
  #
  # #rename for the dwplot
  # names(robust_sign_petition_self_controls_rrponly_remove_baden_df)[1] <- "model"
  # names(robust_sign_petition_self_controls_rrponly_remove_baden_df)[7] <- "term"


  # Personal Views about Appropriateness of Signing Petition - remove_baden only

  # regression for Personal Views about Appropriateness of Signing Petition across different samples
  petition_appropriate_self_controls_fullsample_remove_baden <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_remove_baden)
  petition_appropriate_self_controls_leftonly_remove_baden <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_remove_baden)
  petition_appropriate_self_controls_rightonly_remove_baden <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_remove_baden)
  # petition_appropriate_self_controls_rrponly_remove_baden <- lm(petition_appropriate_self ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_remove_baden)

  # robust SEs
  robust_petition_appropriate_self_controls_fullsample_remove_baden <- summ(petition_appropriate_self_controls_fullsample_remove_baden, robust = "HC1")
  robust_petition_appropriate_self_controls_leftonly_remove_baden <- summ(petition_appropriate_self_controls_leftonly_remove_baden, robust = "HC1")
  robust_petition_appropriate_self_controls_rightonly_remove_baden <- summ(petition_appropriate_self_controls_rightonly_remove_baden, robust = "HC1")
  # robust_petition_appropriate_self_controls_rrponly_remove_baden <- summ(petition_appropriate_self_controls_rrponly_remove_baden, robust = "HC1")

  # convert regression outcomes to df for coefficient plot
  robust_petition_appropriate_self_controls_fullsample_remove_baden_df <- broom::tidy(robust_petition_appropriate_self_controls_fullsample_remove_baden) %>%
    mutate(sample = "Full Sample") %>%
    mutate(measure = "Personal Views of Appropriateness of Signing") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_petition_appropriate_self_controls_fullsample_remove_baden_df)[1] <- "model"
  names(robust_petition_appropriate_self_controls_fullsample_remove_baden_df)[7] <- "term"

  robust_petition_appropriate_self_controls_leftonly_remove_baden_df <- broom::tidy(robust_petition_appropriate_self_controls_leftonly_remove_baden) %>%
    mutate(sample = "Left Wing Only") %>%
    mutate(measure = "Personal Views of Appropriateness of Signing") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_petition_appropriate_self_controls_leftonly_remove_baden_df)[1] <- "model"
  names(robust_petition_appropriate_self_controls_leftonly_remove_baden_df)[7] <- "term"


  robust_petition_appropriate_self_controls_rightonly_remove_baden_df <- broom::tidy(robust_petition_appropriate_self_controls_rightonly_remove_baden) %>%
    mutate(sample = "Right Wing Only") %>%
    mutate(measure = "Personal Views of Appropriateness of Signing") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_petition_appropriate_self_controls_rightonly_remove_baden_df)[1] <- "model"
  names(robust_petition_appropriate_self_controls_rightonly_remove_baden_df)[7] <- "term"

  # robust_petition_appropriate_self_controls_rrponly_remove_baden_df <- broom::tidy(robust_petition_appropriate_self_controls_rrponly_remove_baden) %>%
  #   mutate(sample = "Radical Right Only") %>%
  #   mutate(measure = "Personal Views of Appropriateness of Signing") %>%
  #   filter(term != "age") %>%
  #   filter(term != "household_income")  %>%
  #   filter(term != "education")  %>%
  #   filter(term != "left_right")%>%
  #   filter(term != "(Intercept)")
  #
  # #rename for the dwplot
  # names(robust_petition_appropriate_self_controls_rrponly_remove_baden_df)[1] <- "model"
  # names(robust_petition_appropriate_self_controls_rrponly_remove_baden_df)[7] <- "term"



  # Empirical Expectations - remove_baden only

  # regression for Empirical Expectations across different samples
  sign_petition_others_controls_fullsample_remove_baden <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_remove_baden)
  sign_petition_others_controls_leftonly_remove_baden <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_remove_baden)
  sign_petition_others_controls_rightonly_remove_baden <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_remove_baden)
  # sign_petition_others_controls_rrponly_remove_baden <- lm(sign_petition_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_remove_baden)

  # robust SEs
  robust_sign_petition_others_controls_fullsample_remove_baden <- summ(sign_petition_others_controls_fullsample_remove_baden, robust = "HC1")
  robust_sign_petition_others_controls_leftonly_remove_baden <- summ(sign_petition_others_controls_leftonly_remove_baden, robust = "HC1")
  robust_sign_petition_others_controls_rightonly_remove_baden <- summ(sign_petition_others_controls_rightonly_remove_baden, robust = "HC1")
  # robust_sign_petition_others_controls_rrponly_remove_baden <- summ(sign_petition_others_controls_rrponly_remove_baden, robust = "HC1")

  # convert regression outcomes to df for coefficient plot
  robust_sign_petition_others_controls_fullsample_remove_baden_df <- broom::tidy(robust_sign_petition_others_controls_fullsample_remove_baden) %>%
    mutate(sample = "Full Sample") %>%
    mutate(measure = "Empirical Expectations") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sign_petition_others_controls_fullsample_remove_baden_df)[1] <- "model"
  names(robust_sign_petition_others_controls_fullsample_remove_baden_df)[7] <- "term"


  robust_sign_petition_others_controls_leftonly_remove_baden_df <- broom::tidy(robust_sign_petition_others_controls_leftonly_remove_baden) %>%
    mutate(sample = "Left Wing Only") %>%
    mutate(measure = "Empirical Expectations") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sign_petition_others_controls_leftonly_remove_baden_df)[1] <- "model"
  names(robust_sign_petition_others_controls_leftonly_remove_baden_df)[7] <- "term"


  robust_sign_petition_others_controls_rightonly_remove_baden_df <- broom::tidy(robust_sign_petition_others_controls_rightonly_remove_baden) %>%
    mutate(sample = "Right Wing Only") %>%
    mutate(measure = "Empirical Expectations") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_sign_petition_others_controls_rightonly_remove_baden_df)[1] <- "model"
  names(robust_sign_petition_others_controls_rightonly_remove_baden_df)[7] <- "term"


  # robust_sign_petition_others_controls_rrponly_remove_baden_df <- broom::tidy(robust_sign_petition_others_controls_rrponly_remove_baden) %>%
  #   mutate(sample = "Radical Right Only") %>%
  #   mutate(measure = "Empirical Expectations") %>%
  #   filter(term != "age") %>%
  #   filter(term != "household_income")  %>%
  #   filter(term != "education")  %>%
  #   filter(term != "left_right") %>%
  #   filter(term != "(Intercept)")
  #
  # #rename for the dwplot
  # names(robust_sign_petition_others_controls_rrponly_remove_baden_df)[1] <- "model"
  # names(robust_sign_petition_others_controls_rrponly_remove_baden_df)[7] <- "term"



  # Normative Expectations - remove_baden only

  # regression for Normative Expectations across different samples
  petition_appropriate_others_controls_fullsample_remove_baden <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_remove_baden)
  petition_appropriate_others_controls_leftonly_remove_baden <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_remove_baden)
  petition_appropriate_others_controls_rightonly_remove_baden <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_remove_baden)
  # petition_appropriate_others_controls_rrponly_remove_baden <- lm(petition_appropriate_others ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_remove_baden)

  # robust SEs
  robust_petition_appropriate_others_controls_fullsample_remove_baden <- summ(petition_appropriate_others_controls_fullsample_remove_baden, robust = "HC1")
  robust_petition_appropriate_others_controls_leftonly_remove_baden <- summ(petition_appropriate_others_controls_leftonly_remove_baden, robust = "HC1")
  robust_petition_appropriate_others_controls_rightonly_remove_baden <- summ(petition_appropriate_others_controls_rightonly_remove_baden, robust = "HC1")
  # robust_petition_appropriate_others_controls_rrponly_remove_baden <- summ(petition_appropriate_others_controls_rrponly_remove_baden, robust = "HC1")

  # convert regression outcomes to df for coefficient plot
  robust_petition_appropriate_others_controls_fullsample_remove_baden_df <- broom::tidy(robust_petition_appropriate_others_controls_fullsample_remove_baden) %>%
    mutate(sample = "Full Sample") %>%
    mutate(measure = "Normative Expectations") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_petition_appropriate_others_controls_fullsample_remove_baden_df)[1] <- "model"
  names(robust_petition_appropriate_others_controls_fullsample_remove_baden_df)[7] <- "term"

  robust_petition_appropriate_others_controls_leftonly_remove_baden_df <- broom::tidy(robust_petition_appropriate_others_controls_leftonly_remove_baden) %>%
    mutate(sample = "Left Wing Only") %>%
    mutate(measure = "Normative Expectations") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_petition_appropriate_others_controls_leftonly_remove_baden_df)[1] <- "model"
  names(robust_petition_appropriate_others_controls_leftonly_remove_baden_df)[7] <- "term"

  robust_petition_appropriate_others_controls_rightonly_remove_baden_df <- broom::tidy(robust_petition_appropriate_others_controls_rightonly_remove_baden) %>%
    mutate(sample = "Right Wing Only") %>%
    mutate(measure = "Normative Expectations") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_petition_appropriate_others_controls_rightonly_remove_baden_df)[1] <- "model"
  names(robust_petition_appropriate_others_controls_rightonly_remove_baden_df)[7] <- "term"

  # robust_petition_appropriate_others_controls_rrponly_remove_baden_df <- broom::tidy(robust_petition_appropriate_others_controls_rrponly_remove_baden) %>%
  #   mutate(sample = "Radical Right Only") %>%
  #   mutate(measure = "Normative Expectations") %>%
  #   filter(term != "age") %>%
  #   filter(term != "household_income")  %>%
  #   filter(term != "education")  %>%
  #   filter(term != "left_right")%>%
  #   filter(term != "(Intercept)")
  #
  # #rename for the dwplot
  # names(robust_petition_appropriate_others_controls_rrponly_remove_baden_df)[1] <- "model"
  # names(robust_petition_appropriate_others_controls_rrponly_remove_baden_df)[7] <- "term"



  # Sanctioning - remove_baden only

  # regression for Sanctioning across different samples
  delete_tweet_controls_fullsample_remove_baden <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = analysis_data_remove_baden)
  delete_tweet_controls_leftonly_remove_baden <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = left_only_remove_baden)
  delete_tweet_controls_rightonly_remove_baden <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy + age + household_income + education + left_right, data = right_only_remove_baden)
  # delete_tweet_controls_rrponly_remove_baden <- lm(delete_tweet ~ MRPapprovedummy + RRPapprovedummy + MRPdisapproveRRPapprovedummy + MRPapproveRRPapprovedummy +  age + household_income + education + left_right, data = rrp_only_remove_baden)

  # robust SEs
  robust_delete_tweet_controls_fullsample_remove_baden <- summ(delete_tweet_controls_fullsample_remove_baden, robust = "HC1")
  robust_delete_tweet_controls_leftonly_remove_baden <- summ(delete_tweet_controls_leftonly_remove_baden, robust = "HC1")
  robust_delete_tweet_controls_rightonly_remove_baden <- summ(delete_tweet_controls_rightonly_remove_baden, robust = "HC1")
  # robust_delete_tweet_controls_rrponly_remove_baden <- summ(delete_tweet_controls_rrponly_remove_baden, robust = "HC1")

  # convert regression outcomes to df for coefficient plot
  robust_delete_tweet_controls_fullsample_remove_baden_df <- broom::tidy(robust_delete_tweet_controls_fullsample_remove_baden) %>%
    mutate(sample = "Full Sample") %>%
    mutate(measure = "Sanctioning") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_delete_tweet_controls_fullsample_remove_baden_df)[1] <- "model"
  names(robust_delete_tweet_controls_fullsample_remove_baden_df)[7] <- "term"

  robust_delete_tweet_controls_leftonly_remove_baden_df <- broom::tidy(robust_delete_tweet_controls_leftonly_remove_baden) %>%
    mutate(sample = "Left Wing Only") %>%
    mutate(measure = "Sanctioning") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_delete_tweet_controls_leftonly_remove_baden_df)[1] <- "model"
  names(robust_delete_tweet_controls_leftonly_remove_baden_df)[7] <- "term"


  robust_delete_tweet_controls_rightonly_remove_baden_df <- broom::tidy(robust_delete_tweet_controls_rightonly_remove_baden) %>%
    mutate(sample = "Right Wing Only") %>%
    mutate(measure = "Sanctioning") %>%
    filter(term != "age") %>%
    filter(term != "household_income") %>%
    filter(term != "education") %>%
    filter(term != "left_right") %>%
    filter(term != "(Intercept)")

  # rename for the dwplot
  names(robust_delete_tweet_controls_rightonly_remove_baden_df)[1] <- "model"
  names(robust_delete_tweet_controls_rightonly_remove_baden_df)[7] <- "term"

  # robust_delete_tweet_controls_rrponly_remove_baden_df <- broom::tidy(robust_delete_tweet_controls_rrponly_remove_baden) %>%
  #   mutate(sample = "Radical Right Only") %>%
  #   mutate(measure = "Sanctioning") %>%
  #   filter(term != "age") %>%
  #   filter(term != "household_income")  %>%
  #   filter(term != "education")  %>%
  #   filter(term != "left_right") %>%
  #   filter(term != "(Intercept)")
  #
  # #rename for the dwplot
  # names(robust_delete_tweet_controls_rrponly_remove_baden_df)[1] <- "model"
  # names(robust_delete_tweet_controls_rrponly_remove_baden_df)[7] <- "term"

  # final joining of all the models
  joined_models_controls_remove_baden <- rbind(
    robust_sign_petition_self_controls_fullsample_remove_baden_df,
    robust_sign_petition_self_controls_leftonly_remove_baden_df,
    robust_sign_petition_self_controls_rightonly_remove_baden_df,
    # robust_sign_petition_self_controls_rrponly_remove_baden_df,
    robust_sensitive_item_controls_fullsample_remove_baden_df,
    robust_sensitive_item_controls_leftonly_remove_baden_df,
    robust_sensitive_item_controls_rightonly_remove_baden_df,
    # robust_sensitive_item_controls_rrponly_remove_baden_df,
    robust_petition_appropriate_self_controls_fullsample_remove_baden_df,
    robust_petition_appropriate_self_controls_leftonly_remove_baden_df,
    robust_petition_appropriate_self_controls_rightonly_remove_baden_df,
    # robust_petition_appropriate_self_controls_rrponly_remove_baden_df,
    robust_sign_petition_others_controls_fullsample_remove_baden_df,
    robust_sign_petition_others_controls_leftonly_remove_baden_df,
    robust_sign_petition_others_controls_rightonly_remove_baden_df,
    # robust_sign_petition_others_controls_rrponly_remove_baden_df,
    robust_petition_appropriate_others_controls_fullsample_remove_baden_df,
    robust_petition_appropriate_others_controls_leftonly_remove_baden_df,
    robust_petition_appropriate_others_controls_rightonly_remove_baden_df,
    # robust_petition_appropriate_others_controls_rrponly_remove_baden_df,
    robust_delete_tweet_controls_fullsample_remove_baden_df,
    robust_delete_tweet_controls_leftonly_remove_baden_df,
    robust_delete_tweet_controls_rightonly_remove_baden_df
    # robust_delete_tweet_controls_rrponly_remove_baden_df
  )


  # reorder to specify sequence in facet_wrap
  joined_models_controls_remove_baden$sample <- factor(joined_models_controls_remove_baden$sample, # Reordering group factor levels
    levels = c("Full Sample", "Right Wing Only", "Left Wing Only", "Radical Right Only")
  )

  title_name <- paste0("Treatment Conditions against Control Condition (With Controls): Remove ", state_names[i])

  # final dwplot code
  fig_remove <- dwplot(joined_models_controls_remove_baden,
    vline = geom_vline(
      xintercept = 0,
      colour = "grey60",
      linetype = 2
    ),
    dot_args = list(aes(shape = model)),
    whisker_args = list(aes(linetype = model))
  ) +
    facet_wrap(~sample, nrow = 1) +
    theme(strip.text = element_text(size = 5)) +
    scale_colour_grey(
      start = .1,
      end = .1,
      # if start and end same value, use same colour for all models
      labels = c("MRP Approve and RRP Approve vs Control", "MRP Disapprove and RRP Approve vs Control", "RRP Approve vs Control", "MRP Approve vs Control")
    ) +
    labs(title = title_name) +
    scale_shape_discrete(labels = c("MRP Approve and RRP Approve vs Control", "MRP Disapprove and RRP Approve vs Control", "RRP Approve vs Control", "MRP Approve vs Control")) +
    theme_bw() +
    theme(legend.position = "bottom") +
    theme(legend.text = element_text(size = rel(0.9))) +
    guides(
      shape = guide_legend("Treatment Condition", reverse = TRUE),
      colour = guide_legend("Treatment Condition", reverse = TRUE)
    ) + # Combine the legends for shape and color
    scale_y_discrete(labels = label_wrap(13))

  file_name <- paste0("plots/fig_e", i + 16, ".png")

  ggsave(
    filename = file_name, plot = fig_remove,
    width = 15, height = 10
  )
}
